<?php
add_action( 'admin_init', 'theme_options_init' );
add_action( 'admin_menu', 'theme_options_add_page' );

/**
 * Init plugin options to white list our options
 */
 
function theme_options_init(){
	register_setting( 'blog_options', 'blog_theme_options', 'theme_options_validate' );
}

/**
 * Load up the menu page
*/
function theme_options_add_page() {
	add_theme_page( __( 'گزینه های پوسته', 'blogtheme' ), __( 'گزینه های پوسته', 'blogtheme' ), 'edit_theme_options', 'theme_options', 'theme_options_do_page' );
}

/**
 * Create arrays for our select and radio options
 */

$sidebar_alignment = array(
	'left' => array(
		'value' => 'left',
		'label' => __( 'ستون کناری چپ', 'blogtheme' )
	),
	'right' => array(
		'value' => 'right',
		'label' => __( 'ستون کناری راست', 'blogtheme' )
	)
);

$body_background = array(
	'bg-one' => array(
		'value' => 'bg-one',
		'label' => __('روشن، نویز ظریف', 'blogtheme')
	),
	'bg-two' => array(
		'value' => 'bg-two',
		'label' => __('تیره تر، نویز ظریف', 'blogtheme')
	),
	'bg-three' => array(
		'value' => 'bg-three',
		'label' => __('روشن، بافت - استخوان های کوچک', 'blogtheme')
	),
	'bg-four' => array(
		'value' => 'bg-four',
		'label' => __('روشن، بافت - معبر', 'blogtheme')
	),
	'bg-five' => array(
		'value' => 'bg-five',
		'label' => __('روشن، بافت - دفترچه', 'blogtheme')
	)
);

/**
 * Create the options page
 */
 
function theme_options_do_page() {
	global $select_options, $sidebar_alignment, $body_background;

	if ( ! isset( $_REQUEST['settings-updated'] ) )
		$_REQUEST['settings-updated'] = false;

	?>
	<div class="wrap">
		<?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' گزینه ها', 'blogtheme' ) . "</h2>"; ?>

		<?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
		<div class="updated fade"><p><strong><?php _e( 'گزینه های ذخیره شدند', 'blogtheme' ); ?></strong></p></div>
		<?php endif; ?>

		<form method="post" action="options.php">
			<?php settings_fields( 'blog_options' ); ?>
			<?php $options = get_option( 'blog_theme_options' ); ?>

			<table class="form-table">

				<?php
    /**
     * Authors biography options
     */
    ?>
    <tr valign="top"><th scope="row"><?php _e( 'بیوگرافی نویسنده', 'blogtheme' ); ?></th>
        <td>
            <input id="blog_theme_options[author_bio]" name="blog_theme_options[author_bio]" type="checkbox" value="1" <?php checked( '1', $options['author_bio'] ); ?> />
            <label class="description" for="blog_theme_options[author_bio]"><?php _e( 'نمایش بیوگرافی نویسنده در هر نوشته', 'blogtheme' ); ?></label>
        </td>
    </tr>
    <?php
    /**
     * Authors biography options
     */
    ?>
    <tr valign="top">
    <th scope="row"><?php _e( 'Breadcrumbs', 'blogtheme' ); ?></th>
        <td>
            <input id="blog_theme_options[breadcrumb_post]" name="blog_theme_options[breadcrumb_post]" type="checkbox" value="1" <?php checked( '1', $options['breadcrumb_post'] ); ?> />
            <label class="description" for="blog_theme_options[breadcrumb_post]"><?php _e( 'نمایش Breadcrumbs در نوشته', 'blogtheme' ); ?></label>
            <br />
        </td>
    </tr>
    <tr>
	    <th></th>
	    <td>
       		<input id="blog_theme_options[breadcrumb_page]" name="blog_theme_options[breadcrumb_page]" type="checkbox" value="1" <?php checked( '1', $options['breadcrumb_page'] ); ?> />
            <label class="description" for="blog_theme_options[breadcrumb_page]"><?php _e( 'نمایش Breadcrumbs در تک برگه', 'blogtheme' ); ?></label>
       	</td>
    <tr></tr>
       	<th></th>
	    <td>
       		<input id="blog_theme_options[breadcrumb_filter]" name="blog_theme_options[breadcrumb_filter]" type="checkbox" value="1" <?php checked( '1', $options['breadcrumb_filter'] ); ?> />
            <label class="description" for="blog_theme_options[breadcrumb_filter]"><?php _e( 'نمایش Breadcrumbs در برگه های فیلتر شده و جستجو', 'blogtheme' ); ?></label>
       	</td>
    </tr>
			
				<?php
				
				/**
				 * Backgrounds 
				 */
				?>
				<tr valign="top"><th scope="row"><?php _e( 'پس زمینه:', 'blogtheme' ); ?></th>
					<td>
						<fieldset><legend class="screen-reader-text"><span><?php _e( 'Radio buttons', 'blogtheme' ); ?></span></legend>
						<?php
							if ( ! isset( $checked ) )
								$checked = '';
							foreach ( $body_background as $option ) {
								$radio_setting = $options['body_background'];

								if ( '' != $radio_setting ) {
									if ( $options['body_background'] == $option['value'] ) {
										$checked = "checked=\"checked\"";
									} else {
										$checked = '';
									}
								}
								?>
								<label class="description"><input type="radio" name="blog_theme_options[body_background]" value="<?php esc_attr_e( $option['value'] ); ?>" <?php echo $checked; ?> /> <?php echo $option['label']; ?><br /></label>
								<?php
							}
						?>
						</fieldset>
					</td>
				</tr>
				
				<?php
				/*
				Custom background
				*/
				?>
				
				<tr valign="top"><th scope="row"><?php _e( 'پیوند پس زمینه خود را وارد کنید.', 'blogtheme' ); ?></th>
                    <td>
                        <input id="blog_theme_options[custom_bg]" class="regular-text" type="text" name="blog_theme_options[custom_bg]" value="<?php esc_attr_e( $options['custom_bg'] ); ?>" />
                        <label class="description" for="blog_theme_options[custom_bg]"><?php _e( 'مانند: /wordpress/wp-content/uploads/2012/02/bg-one.jpg', 'blogtheme' ); ?></label>
                    </td>
                </tr>
				
				<?php
				
				/**
				 * Location of sidebar
				 */
				?>
				<tr valign="top"><th scope="row"><?php _e( 'مکان ستون کناری:', 'blogtheme' ); ?></th>
					<td>
						<fieldset><legend class="screen-reader-text"><span><?php _e( 'Radio buttons', 'blogtheme' ); ?></span></legend>
						<?php
							if ( ! isset( $checked ) )
								$checked = '';
							foreach ( $sidebar_alignment as $option ) {
								$radio_setting = $options['sidebar_align'];

								if ( '' != $radio_setting ) {
									if ( $options['sidebar_align'] == $option['value'] ) {
										$checked = "checked=\"checked\"";
									} else {
										$checked = '';
									}
								}
								?>
								<label class="description"><input type="radio" name="blog_theme_options[sidebar_align]" value="<?php esc_attr_e( $option['value'] ); ?>" <?php echo $checked; ?> /> <?php echo $option['label']; ?></label><br />
								<?php
							}
						?>
						</fieldset>
					</td>
				</tr>
				
				<?php
				/**
				 * Google analytics
				 */
				?>
				<tr valign="top"><th scope="row"><?php _e( 'کد آمارگیر گوگل (یا دگری) را اینجا وارد کنید. آن قبل از بسته شدن تگ بدنه درج خواهد شد..', 'blogtheme' ); ?></th>
					<td>
						<textarea id="blog_theme_options[analytics]" class="large-text" cols="25" rows="5" name="blog_theme_options[analytics]"><?php echo esc_textarea( $options['analytics'] ); ?></textarea>
					</td>
				</tr>
				<?php
				/**
				 * Footer text
				 */
				?>				
				<tr valign="top"><th scope="row"><?php _e( 'متن فوتر خود را اینجا وارد کنید.', 'blogtheme' ); ?></th>
					<td>
						<textarea id="blog_theme_options[footer_text]" class="large-text" cols="25" rows="5" name="blog_theme_options[footer_text]"><?php echo esc_textarea( $options['footer_text'] ); ?></textarea>
					</td>
				</tr>
			</table>

			<p class="submit">
				<input type="submit" class="button-primary" value="<?php _e( 'ذخیره گرینه ها', 'blogtheme' ); ?>" />
			</p>
		</form>
	</div>
	<?php
}

/**
 * Sanitize and validate input. Accepts an array, return a sanitized array.
 */
function theme_options_validate( $input ) {
	global $select_options, $sidebar_alignment, $body_background;

	// Validate author bio
	if ( ! isset( $input['author_bio'] ) )
		$input['author_bio'] = null;
	$input['author_bio'] = ( $input['author_bio'] == 1 ? 1 : 0 );
	
	// Validate breadcrumbs on post
	if ( ! isset( $input['breadcrumb_post'] ) )
		$input['breadcrumb_post'] = null;
	$input['breadcrumb_post'] = ( $input['breadcrumb_post'] == 1 ? 1 : 0 );
	
	// Validate breadcrumbs on page
	if ( ! isset( $input['breadcrumb_page'] ) )
		$input['breadcrumb_page'] = null;
	$input['breadcrumb_page'] = ( $input['breadcrumb_page'] == 1 ? 1 : 0 );

	// Validate breadcrumbs on filtered
	if ( ! isset( $input['breadcrumb_filter'] ) )
		$input['breadcrumb_filter'] = null;
	$input['breadcrumb_filter'] = ( $input['breadcrumb_filter'] == 1 ? 1 : 0 );
	
	// Validate of sidebar align
	if ( ! isset( $input['sidebar_align'] ) )
		$input['sidebar_align'] = null;
	if ( ! array_key_exists( $input['sidebar_align'], $sidebar_alignment ) )
		$input['sidebar_align'] = null;

	// Background validate
	if ( ! isset( $input['body_background'] ) )
		$input['body_background'] = null;
	if ( ! array_key_exists( $input['body_background'], $body_background ) )
		$input['body_background'] = null;
	
	// Validate custom background
    $input['custom_bg'] = wp_filter_nohtml_kses( $input['custom_bg'] );

	return $input;
}

// adapted from http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/